package com.gitee.common.data.tree;

import com.gitee.common.data.tree.node.*;

public class TreeData {

    private TreeItemNodeI rootNode;
    private TreeItemNodeI currentNode;

    public void init(String json) {

    }

    public TreeItemNodeI appendLeaf(TreeItemNodeData nodeData, int nodeType) {
        if (currentNode == null) {
            currentNode = rootNode;
        }
        if (currentNode instanceof NextTreeItemNodeI) {
            AbstractTreeItemNode instance = TreeNodeFactory.createInstance(nodeType);
            instance.setNodeData(nodeData);
            ((NextTreeItemNodeI) currentNode).append(instance);
            return instance;
        }
        return null;
    }

    private TreeItemNodeI appendLeaf(FrontTreeItemNodeI frontNode) {
        if (currentNode == null) {
            currentNode = rootNode;
        }
        if (currentNode instanceof NextTreeItemNodeI) {
            ((NextTreeItemNodeI) currentNode).append(frontNode);
        }
        return frontNode;
    }

    public void appendTree(TreeData treeData) {
        if (treeData.getRootNode() instanceof FrontTreeItemNodeI) {
            appendLeaf((FrontTreeItemNodeI) treeData.getRootNode());
        }
    }

    public void insertBefore(TreeItemNodeData nodeData, int nodeType, String id) {
        if (currentNode == null) {
            currentNode = rootNode;
        }
        if (currentNode instanceof NextTreeItemNodeI) {
            if (currentNode instanceof OneNextTreeItemNodeI) {
                TreeItemNodeI next = ((OneNextTreeItemNodeI) currentNode).getNext();

            }
        }
    }

    public void insertAfter(TreeItemNodeData nodeData, String id) {
        if (currentNode == null) {
            currentNode = rootNode;
        }
        TreeItemNodeI cnode = currentNode;
        if (currentNode instanceof NextTreeItemNodeI) {
            if (currentNode instanceof OneNextTreeItemNodeI) {
                TreeItemNodeI next = ((OneNextTreeItemNodeI) currentNode).getNext();

                AbstractTreeItemNode instance = TreeNodeFactory.createInstance(TreeNodeFactory.MORE_MORE);
                instance.setNodeData(nodeData);

                this.appendLeaf((FrontTreeItemNodeI) instance);
                setCurrentNode(instance);
                this.appendLeaf((FrontTreeItemNodeI) next);

                setCurrentNode(cnode);
            }
        }
    }

    /*public void insertAfter(TreeItemNodeData nodeData, int nodeType, String id) {
        if (currentNode == null) {
            currentNode = rootNode;
        }
        if (currentNode instanceof NextTreeItemNodeI) {
            if (currentNode instanceof OneNextTreeItemNodeI) {
                TreeItemNodeI next = ((OneNextTreeItemNodeI) currentNode).getNext();

                AbstractTreeItemNode instance = TreeNodeFactory.createInstance(nodeType);
                instance.setNodeData(nodeData);
                TreeData newTree = new TreeData();
                newTree.setRootNode(instance);
                newTree.appendLeaf((FrontTreeItemNodeI) next);

                this.appendTree(newTree);
            }
        }
    }*/

    public TreeItemNodeI getRootNode() {
        return rootNode;
    }

    public void setRootNode(TreeItemNodeI rootNode) {
        this.rootNode = rootNode;
    }

    public TreeItemNodeI getCurrentNode() {
        return currentNode;
    }

    public void setCurrentNode(TreeItemNodeI currentNode) {
        this.currentNode = currentNode;
    }
}
